#include "os_sal.h"


/* os task 任务 */

#if (CONFIG_OS_ENABLE == 1)


/* export func */

#if defined(CONFIG_OS_TYPE_FREERTOS)

#include "FreeRTOS.h"
#include "task.h"

/**
 * @brief OS创建任务
 * 
 * @param task 任务入口函数
 * @param param 任务入口函数形参
 * @param name 任务名称
 * @param stk_size 任务栈大小(字)
 * @param prio 任务优先级
 * @param handle 任务句柄
 * @return os_status_t 
 */
os_status_t os_task_create(void (*task)(void *param),
                           void *param,
                           const char *name,
                           uint32_t stk_size,
                           uint32_t prio,
                           os_task_handle_t handle)
{
    os_status_t os_sta;
    BaseType_t err;

    err = xTaskCreate((TaskFunction_t)task,
                      (const char *)name,
                      (uint16_t)stk_size,
                      (void *)param,
                      (UBaseType_t)prio,
                      (TaskHandle_t *)handle);

    if (err == pdTRUE) os_sta = OS_STA_SUCCESS;
    else
    {
        os_sta = OS_STA_ERROR;
        printf("task %s create fail\r\n", name);
    }

    return os_sta;
}

/**
 * @brief OS删除任务
 * 
 * @param handle 任务句柄,当为NULL时当前任务
 * @param freefun 删除后执行的释放函数
 * @return os_status_t 
 */
os_status_t os_task_delete(os_task_handle_t handle, void (*freefun)(void))
{
    vTaskDelete(handle);
    if (freefun != NULL)
    {
        freefun();
    }

    return OS_STA_SUCCESS;
}

/**
 * @brief OS挂起任务
 * 
 * @param handle 任务句柄,当为NULL时为当前任务
 * @return os_status_t 
 */
os_status_t os_task_suspend(os_task_handle_t handle)
{
	vTaskSuspend(handle);

	return OS_STA_SUCCESS;
}

/**
 * @brief OS恢复任务
 * 
 * @param handle 任务句柄，不能为NULL
 * @return os_status_t 
 */
os_status_t os_task_resume(os_task_handle_t handle)
{
	if (handle == NULL) return OS_STA_ERROR;
	vTaskResume(handle);

	return OS_STA_SUCCESS;
}


#else
    #error "CONFIG_OS_TYPE not support!"
#endif // CONFIG_OS_TYPE

#endif // CONFIG_OS_ENABLE
